package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SysExamineServiceImpl implements ISysExamineService {

    @Autowired
    private SysFormMapper sysFormMapper;

    @Autowired
    private SysFormPlanMapper sysFormPlanMapper;

    @Autowired
    private SysFormOptionMapper sysFormOptionMapper;

    @Autowired
    private SysFormHeadMapper sysFormHeadMapper;

    @Autowired
    private SysFormDetailMapper sysFormDetailMapper;

    @Autowired
    private SysFormDetailOptionMapper sysFormDetailOptionMapper;

    @Override
    public AjaxResult formContent(String formName) {


//        SysFormPlan sysFormPlan = sysFormPlanMapper.selectSysFormPlanByPlanId(dto.getId());


        List<SysForm> formList = sysFormMapper.selectFromByFormName(formName);


        Map<Long, String> formDetailMap = formList.stream().collect(Collectors.toMap(SysForm::getFormId, SysForm::getCheckType));

        Map<Long, List<SysForm>> formInspectMap = formList.stream().collect(Collectors.groupingBy(SysForm::getFormId));

        List<Map<String, Object>> dataList = new ArrayList<>();


        formInspectMap.entrySet().forEach(d -> {

            Map<String, Object> detailMap = new HashMap<>();

            Long detailId = d.getKey();

            detailMap.put("detailId", String.valueOf(detailId));

            String checkType = formDetailMap.get(detailId);
            detailMap.put("checkType", checkType);

            List<SysFormOption> options = sysFormOptionMapper.selectListByDetailId(detailId);

            List<Map<String, Object>> optionList = new ArrayList<>();

            for (SysFormOption option : options) {

                Map<String, Object> optionMap = new HashMap<>();
                optionMap.put("optionId", String.valueOf(option.getOptionId()));
                optionMap.put("deduction", option.getDeduction());
                optionMap.put("groupId", option.getGroupId());
                optionMap.put("optionValue", 0);

                optionList.add(optionMap);
            }

            detailMap.put("optionList", optionList);
            detailMap.put("problem", "");
            detailMap.put("remark", "");

            dataList.add(detailMap);
        });
        Collections.reverse(dataList);
        return AjaxResult.success(dataList);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult examine(ExamineFormDTO dto) {


        SysFormHead head = new SysFormHead();
        head.setInspectionId(dto.getInspectionId());
        head.setInspectionPerson(dto.getInspectionPerson());
        sysFormHeadMapper.insertSysFormHead(head);

        List<SysFormDetail> detailList = new ArrayList<>();
        List<SysFormDetailOption> optionList = new ArrayList<>();


        for (ExamineFormDetailDTO detailVo : dto.getAssessmentItems()) {

            SysFormDetail detailPo = new SysFormDetail();

            long examineDetailId = IdUtil.getSnowflakeNextId();
            detailPo.setDetailRecordId(examineDetailId);
            detailPo.setDetailId(detailVo.getDetailId());
            detailPo.setProblem(detailVo.getProblem());
            detailPo.setRemark(detailVo.getRemark());
            detailPo.setInspectionId(dto.getInspectionId());
            detailPo.setHeadId(head.getHeadId());
            detailList.add(detailPo);

            sysFormDetailMapper.insertSysFormDetail(detailPo);

            for (ExamineOptionDTO optionVo : detailVo.getOptionList()) {

                SysFormDetailOption optionPo = new SysFormDetailOption();
                optionPo.setOptionId(optionVo.getOptionId());
                optionPo.setInspectionId(dto.getInspectionId());
                optionPo.setDeduction(optionVo.getDeduction());
                optionPo.setGroupId(optionVo.getGroupId());
                optionPo.setExamineDetailId(detailPo.getDetailRecordId());
                optionPo.setOptionValue(optionVo.getOptionValue());
                optionList.add(optionPo);

            }

        }

        if (CollUtil.isNotEmpty(optionList)) {
            sysFormDetailOptionMapper.batchInsert(optionList);
        }


        SysFormPlan sysFormPlan = new SysFormPlan();
        sysFormPlan.setPlanId(dto.getInspectionId());
        sysFormPlan.setStatus("1");
        sysFormPlanMapper.updateSysFormPlan(sysFormPlan);


        return AjaxResult.success();
    }


    @Override
    public AjaxResult detail(IdDto dto) {

        Map<String,Object> result = new HashMap<>();

        SysFormHead sysFormHead = sysFormHeadMapper.selectSysFormHeadByInspectId(dto.getId());

        result.put("inspectionPerson",sysFormHead.getInspectionPerson());

        SysFormDetail formDetail = new SysFormDetail();
        formDetail.setHeadId(sysFormHead.getHeadId());
        List<SysFormDetail> detailList = sysFormDetailMapper.selectSysFormDetailList(formDetail);

        List<Long> detailIdList = detailList.stream().map(SysFormDetail::getDetailRecordId).collect(Collectors.toList());


        List<SysFormDetailOption> examineOptionList = sysFormDetailOptionMapper.selectSysFormDetailOptionListByDetailIdList(detailIdList);

        Map<Long, SysFormDetailOption> examineOptionMap = examineOptionList.stream().collect(Collectors.toMap(SysFormDetailOption::getOptionId, Function.identity()));

        Map<Long, SysFormDetail> examineDetailMap = detailList.stream().collect(Collectors.toMap(SysFormDetail::getDetailId, Function.identity()));

        SysFormPlan formPlanDo = sysFormPlanMapper.selectSysFormPlanByPlanId(dto.getId());

        SysForm sysForm = new SysForm();
        sysForm.setFormName(formPlanDo.getFormName());
        List<SysForm> formList = sysFormMapper.selectSysFormList(sysForm);


        Map<Long, String> formDetailMap = formList.stream().collect(Collectors.toMap(SysForm::getFormId, SysForm::getCheckType));

        Map<Long, List<SysForm>> formInspectMap = formList.stream().collect(Collectors.groupingBy(SysForm::getFormId));

        List<Map<String,Object>> dataList = new ArrayList<>();


        formInspectMap.entrySet().forEach(d->{


            Map<String,Object> detailMap = new HashMap<>();

            Long detailId = d.getKey();
            detailMap.put("detailId",String.valueOf(detailId));

            String checkType = formDetailMap.get(detailId);
            detailMap.put("checkType",checkType);

            List<SysFormOption> options = sysFormOptionMapper.selectListByDetailId(detailId);


            List<Map<String,Object>> optionList = new ArrayList<>();

            for (SysFormOption option : options) {


                Map<String ,Object> optionMap = new HashMap<>();
                optionMap.put("optionId",String.valueOf(option.getOptionId()));
                optionMap.put("deduction",option.getDeduction());
                optionMap.put("groupId",option.getGroupId());

                SysFormDetailOption optionData = examineOptionMap.get(Long.valueOf(option.getOptionId()));

                optionMap.put("optionValue",optionData.getOptionValue());

                optionList.add(optionMap);

            }

            detailMap.put("optionList",optionList);

            SysFormDetail detailPo = examineDetailMap.get(detailId);


            detailMap.put("problem", detailPo.getProblem());
            detailMap.put("remark", detailPo.getRemark());

            dataList.add(detailMap);
        });

        Collections.reverse(dataList);

        result.put("dataList",dataList);

        return AjaxResult.success(result);
    }
}
